package ren.xbcr.schoolbus.client.util;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

import ren.xbcr.schoolbus.client.LoginActivity;
import ren.xbcr.schoolbus.client.R;
import ren.xbcr.schoolbus.client.domain.BusDriver;
import ren.xbcr.schoolbus.client.domain.Car;
import ren.xbcr.schoolbus.client.domain.ValidationMark;

/**
 * Created by Jian on 2015/12/3.
 */
public class Tool {
    public static void setCarsToMap(AMap aMap, ArrayList<BusDriver> busDrivers, ArrayList<Marker> markers) {
        ArrayList<Marker> hasMarkers = new ArrayList<Marker>();
        for (BusDriver busDriver :
                busDrivers) {
            Marker marker = containCar(markers, busDriver);
            Car car = busDriver.getDriveCar();
            if (marker == null) {
//                if (!car.getIsActive()) {
//                    continue;
//                }
                Marker newMarker = createMarker(aMap, busDriver, car);
                markers.add(newMarker);
                hasMarkers.add(newMarker);
            } else {
                hasMarkers.add(marker);
                /*if (!car.getIsActive()) {
                    markers.remove(marker);
                    marker.destroy();
                } else {
                    marker.setPosition(new LatLng(busDriver.getLatitude(), busDriver.getLongitude()));
                }*/
                marker.setPosition(new LatLng(busDriver.getLatitude(), busDriver.getLongitude()));

            }
        }

        deleteDisappearMarkers(markers, hasMarkers);
    }


    public static void showToast(Context context, String message) {
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, message, duration);
        toast.show();
    }

    @NonNull
    public static String verifyTelephone(Context context, ValidationMark validationMark, EditText mTelephoneEditor) {
        String telephone = mTelephoneEditor.getText().toString();
        if (!validationMark.getCancel() && (telephone == null || telephone.isEmpty())) {
            mTelephoneEditor.setError(context.getString(R.string.error_telephone_empty));
            validationMark.setFocusView(mTelephoneEditor);
            validationMark.setCancel(true);
        }
        if (!validationMark.getCancel() && (telephone.length() != 11)) {
            mTelephoneEditor.setError(context.getString(R.string.error_telephone_format));
            validationMark.setFocusView(mTelephoneEditor);
            validationMark.setCancel(true);
        }
        return telephone;
    }

    public static XbcrResult createErrorResult(String message) {
        XbcrResult xbcrResult = new XbcrResult();
        xbcrResult.setResultCode(XbcrResult.RESULT_ERROR);
        xbcrResult.setResultMessage(message);
        return xbcrResult;
    }



    public static final int LOGIN_REQUEST = 1;  // The request code

    public static boolean navToLoginActivity(Context context, FragmentActivity activity) {
        String telephone = PrefUtils.getStringFromPrefs(context, PrefUtils.PREFS_LOGIN_TELEPHONE, null);
        if (telephone == null || telephone.isEmpty()) {
            Intent intent = new Intent(context, LoginActivity.class);
            activity.startActivityForResult(intent, LOGIN_REQUEST);
            return true;
        }
        return false;
    }

    public static XbcrResult createSuccessResult() {
        XbcrResult xbcrResult = new XbcrResult();
        xbcrResult.setResultCode(XbcrResult.RESULT_SUCCESS);
        return xbcrResult;
    }


    public  static String getNationalNumber(String tel){
        if (tel.startsWith("+86")){
            return tel.replace("+86", "");
        }else if(tel.length() != 11){

            return "";
        }else{
            return tel;
        }
    }

    private final static AtomicInteger c = new AtomicInteger(0);
    public static int getNotificationID () {
        return c.incrementAndGet();
    }

    private static Marker containCar(ArrayList<Marker> markers, BusDriver busDriver) {
        for (Marker marker : markers) {
            String title = marker.getTitle();
            if (title != null && title.equalsIgnoreCase(busDriver.getDriveCar().getIdentityNum())) {
                return marker;
            }
        }
        return null;
    }

    private static Marker createMarker(AMap aMap, BusDriver busDriver, Car car) {
        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_car);
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.title(car.getIdentityNum());
        markerOptions.setFlat(true);
        markerOptions.anchor(0.5f, 0.5f);
        markerOptions.icon(bitmapDescriptor);
        markerOptions.position(new LatLng(busDriver.getLatitude(), busDriver.getLongitude()));
        return aMap.addMarker(markerOptions);
    }

    private static void deleteDisappearMarkers(ArrayList<Marker> markers, ArrayList<Marker> hasMarkers) {
        ArrayList<Marker> deletedMarkers = new ArrayList<Marker>();
        for (Marker marker : markers) {
            if (!hasMarkers.contains(marker)) {
                deletedMarkers.add(marker);
            }
        }
        for (Marker marker : deletedMarkers) {
            markers.remove(marker);
            marker.destroy();
        }
    }

}
